package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Employee;
import com.wn55.hrms.entity.RecruitChannel;
import com.wn55.hrms.service.EmployeeService;
import com.wn55.hrms.service.RecruitChannelService;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.catalina.Manager;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Mr.bai~
 * @version 1.0
 * @date 2022/4/28 11:49
 */
@RestController
@RequestMapping("channel")
@Api(value = "本接口是用来操作渠道功能")
public class RecruitChannelController {

    @Autowired
    RecruitChannelService recruitChannelService;

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/channelList")
    @ApiOperation(value = "分页查询渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "查询当前页",required = false,defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value ="查询每页条数",required = false,defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="channelName",value = "渠道种类",required = false),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="channelType",value = "渠道名称",required = false),
    })
    public ResultResponse<PageInfo<RecruitChannel>> channelList(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "2") Integer size,
            String channelName, String channelType) {
        System.out.println(channelName);
        System.out.println(channelType);
        ResultResponse<PageInfo<RecruitChannel>> result = null;
        try {

            PageInfo<RecruitChannel> pageInfo = recruitChannelService.queryChannel(now, size, channelName, channelType);
            result = new ResultResponse<>(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(404, "查询失败");
        }
        return result;
    }

    @PostMapping("/addChannel")
    @ApiOperation(value = "添加渠道信息")
    public ResultResponse<Void> addChannel(@RequestBody RecruitChannel recruitChannel) {
        ResultResponse<Void> result = null;
        try {
            recruitChannel.setInsertTime(new Date());
            System.out.println(recruitChannel.getCreateName());
            recruitChannel.setUpdateTime(new Date());
            recruitChannelService.addChannel(recruitChannel);
            result = new ResultResponse<>(200, "添加角色成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加角色失败!");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据渠道标识ID删除渠道")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "int",name = "id",value = "渠道的ID标识")
    })
    public ResultResponse<Void> delete(@PathVariable("id") int id) {

        ResultResponse<Void> result = null;

        try {
            recruitChannelService.removeBykey(id);
            result = new ResultResponse<>(200, "删除角色成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除角色失败!");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改渠道信息")
    public ResultResponse<Void> editChannel(@RequestBody RecruitChannel recruitChannel){
        ResultResponse<Void> result = null;
        try{
            recruitChannel.setUpdateTime(new Date());
           // recruitChannel.setUpdateName("admin");
            recruitChannel.setUpdateName(recruitChannel.getCreateName());
            recruitChannelService.modify(recruitChannel);
           ;
            result = new ResultResponse<>(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(404,"修改失败");
        }
        return result;
    }
}

